#include <sc95f.h>
#include <sc95f_iap.h>
#include "User/Save/Save.h"
#include "User/OLED/OLED.h"
#include "User/Hint/Hint.h"
#include "User/Clock/Clock.h"
#include "User/KEY/KEY.h"
#include "User/Infrared/Infrared.h"
#include "User/Communicate/Communicate.h"
#include "User/Page/Page.h"
#include "User/Ctrl/Ctrl.h"

/* 自定义控制位 */
static uint32_t state_flag;

#define Index_Flag (BIT(0))
#define SelectPage_Flag (BIT(1))
#define FoodOpen_Flag (BIT(2))
#define AutoFeed_Flag (BIT(3))
#define SetFeedTimePage_Flag (BIT(4))
#define Sound_Flag (BIT(5))

/* 012必须是连续的三位 */
#define FeedTime0On_Flag (BIT(6))
#define FeedTime1On_Flag (BIT(7))
#define FeedTime2On_Flag (BIT(8))

#define FeedTimeHour_Flag (BIT(9))
#define FeedTimeMinute_Flag (BIT(10))
#define FeedTimeSecond_Flag (BIT(11))

#define WaterOpen_Flag (BIT(12))
#define forceOpen_Flag (BIT(13))
#define SetFeedSizePage_Flag (BIT(14))
#define FoodOpenWorking_Flag (BIT(15))
#define WaterOpenWorking_Flag (BIT(16))
#define ChildLock_Flag (BIT(17))

#define MusicUp_Flag (BIT(18))
#define MusicDown_Flag (BIT(19))

#define Main_FlagShift(flag)  (CHANGE_BIT(state_flag, flag))
#define Main_FlagSet(flag)    (SET_BIT(state_flag, flag))
#define Main_FlagReset(flag)  (CLEAR_BIT(state_flag, flag))
#define Main_FlagGet(flag)    (IS_BIT_1(state_flag, flag))

#define MAIN_TEST true

/* 自定义全局变量 */
uint8_t str[30];
uint8_t feedTime[][3] = {{8, 0, 0},{12, 0, 0},{16, 53, 58}};
uint8_t nowtime[3] = {16,54,0};
uint8_t feedSize[2] = {9, 4};
uint8_t sound_flag;

/* 自定义函数 */
void main_savedata();
void main_readdata();
void main_dealCommunicate(uint8_t now_Uart);
bool main_time2feed();

void main()
{
    uint8_t i=0;
    uint8_t k = 0; /* 按键符 */
    uint32_t key_scan_mills = 0;
    uint8_t water_state = 0;
    uint8_t page_select;
    uint8_t feed_time_select;
    uint8_t music_up = 5;
    uint8_t music_down = 5;
    uint32_t sound_time = 0;
    uint32_t feed_time = 0;
    uint32_t water_time = 0;
    uint32_t no_food_detect_time = 0;
    uint8_t child_unlock = 0;
    uint32_t com_mills = 0;
    uint32_t last_feedtime = 0;
    uint32_t music_time=0;
    state_flag = 1;
    P3CON |= 0x02;
    Hint_init();
    main_readdata();
    OLED_Init();
    Clock_init();
    KEY_Init();
    Infrared_init();
    Communicate_init(Computer_Uart);
    Communicate_init(ESP32_Uart);
    Communicate_initSet(WaterDetector_Uart, 32000000, 9600, UART_Mode_10B, 0, UART_RX_ENABLE);
    Page_indexInit();
    Ctrl_init();
    
    while(1)
    {
        /* 按键扫描部分 */
        if(Clock_mills() - key_scan_mills > 20)
        {
            k = KEY_get();
            if(k != notouch)
            {
                sound_flag = true;
            }
            if (k == CONFIRM_KEY && !Main_FlagGet(ChildLock_Flag))
            {
                /* 选择功能页面 */
                if(Main_FlagGet(SelectPage_Flag))
                {
                    if(page_select == autoFeed)
                    {
                        Main_FlagShift(AutoFeed_Flag);
                        if(Main_FlagGet(AutoFeed_Flag))
                        {
                            Page_FlagSet(PageAutoFeed_Flag);
                        }
                        else
                        {
                            Page_FlagReset(PageAutoFeed_Flag);
                        }
                    }
                    /* 跳转到设置放养时间页面 */
                    else if(page_select == setFeedTime)
                    {
                        Main_FlagReset(SelectPage_Flag);
                        Main_FlagSet(SetFeedTimePage_Flag);
                        feed_time_select = 0;
                        Page_feedTimeInit();
                        Main_FlagReset(FeedTimeHour_Flag);
                        Page_FlagReset(PageFeedTimeHour_Flag);
                        Main_FlagReset(FeedTimeMinute_Flag);
                        Page_FlagReset(PageFeedTimeMinute_Flag);
                        Main_FlagReset(FeedTimeSecond_Flag);
                        Page_FlagReset(PageFeedTimeSecond_Flag);
                        if(Main_FlagGet(FeedTime0On_Flag))
                        {
                            Page_FlagSet(PageFeedTime0On_Flag);
                        }
                        else
                        {
                            Page_FlagReset(PageFeedTime0On_Flag);
                        }
                        if(Main_FlagGet(FeedTime1On_Flag))
                        {
                            Page_FlagSet(PageFeedTime1On_Flag);
                        }
                        else
                        {
                            Page_FlagReset(PageFeedTime1On_Flag);
                        }
                        if(Main_FlagGet(FeedTime2On_Flag))
                        {
                            Page_FlagSet(PageFeedTime2On_Flag);
                        }
                        else
                        {
                            Page_FlagReset(PageFeedTime2On_Flag);
                        }
                    }
                    else if(page_select == sountTurn)
                    {
                        Main_FlagShift(Sound_Flag);
                        if(Main_FlagGet(Sound_Flag))
                        {
                            Page_FlagSet(PageSound_Flag);
                        }
                        else
                        {
                            Page_FlagReset(PageSound_Flag);
                        }
                        Hint_soundShift();
                    }
                    else if(page_select == setFeedSize)
                    {
                        Main_FlagReset(SelectPage_Flag);
                        Main_FlagSet(SetFeedSizePage_Flag);
                        feed_time_select = 1;
                        Page_feedSizeInit();
                    }
                    else if(page_select == childLockTurn)
                    {
                        Main_FlagSet(ChildLock_Flag);
                        Page_FlagSet(PageChildLock_Flag);
                        child_unlock = 0;
                    }
                }
                /* 设置喂养时间页面 */
                else if(Main_FlagGet(SetFeedTimePage_Flag))
                {
                    if(feed_time_select == 0)
                    {
                        if(Main_FlagGet(FeedTimeHour_Flag))
                        {
                            Main_FlagReset(FeedTimeHour_Flag);
                            Page_FlagReset(PageFeedTimeHour_Flag);
                            Main_FlagSet(FeedTimeMinute_Flag);
                            Page_FlagSet(PageFeedTimeMinute_Flag);
                        }
                        else if(Main_FlagGet(FeedTimeMinute_Flag))
                        {
                            Main_FlagReset(FeedTimeMinute_Flag);
                            Page_FlagReset(PageFeedTimeMinute_Flag);
                            Main_FlagSet(FeedTimeSecond_Flag);
                            Page_FlagSet(PageFeedTimeSecond_Flag);
                        }
                        else if(Main_FlagGet(FeedTimeSecond_Flag))
                        {
                            Main_FlagReset(FeedTimeSecond_Flag);
                            Page_FlagReset(PageFeedTimeSecond_Flag);
                        }
                        else
                        {
                            Main_FlagSet(FeedTimeHour_Flag);
                            Page_FlagSet(PageFeedTimeHour_Flag);
                        }
                    }
                    else 
                    {
                        if(Main_FlagGet(FeedTimeHour_Flag))
                        {
                            feedTime[feed_time_select - 1][0] += 1;
                            feedTime[feed_time_select - 1][0] %= 24;
                            Page_feedTimeFresh();
                        }
                        else if(Main_FlagGet(FeedTimeMinute_Flag))
                        {
                            feedTime[feed_time_select - 1][1] += 1;
                            feedTime[feed_time_select - 1][1] %= 60;
                            Page_feedTimeFresh();
                        }
                        else if(Main_FlagGet(FeedTimeSecond_Flag))
                        {
                            feedTime[feed_time_select - 1][2] += 1;
                            feedTime[feed_time_select - 1][2] %= 60;
                            Page_feedTimeFresh();
                        }
                        else
                        {
                            Main_FlagShift(FeedTime0On_Flag << (feed_time_select - 1));
                            if(Main_FlagGet(FeedTime0On_Flag << (feed_time_select - 1)))
                            {
                                Page_FlagSet(PageFeedTime0On_Flag << (feed_time_select - 1));
                            }
                            else
                            {
                                Page_FlagReset(PageFeedTime0On_Flag << (feed_time_select - 1));
                            }
                        }
                    }
                }
                else if(Main_FlagGet(SetFeedSizePage_Flag))
                {
                    if(feed_time_select == 1)
                    {
                        feedSize[0] += 1;
                        if(feedSize[0]>=10)
                        {
                            feedSize[0] = 1;
                        }
                    }
                    else if(feed_time_select == 2)
                    {
                        feedSize[1] += 1;
                        if(feedSize[1]>=5)
                        {
                            feedSize[1] = 1;
                        }
                    }
                    Page_feedSizeFresh();
                }
            }
            else if(k == SETTING_KEY)
            {
                /* 主页向设置页 */
                if(Main_FlagGet(Index_Flag))
                {
                    Main_FlagReset(Index_Flag);
                    Main_FlagSet(SelectPage_Flag);
                    Page_selectInit();
                    page_select = 0;
                    if(Main_FlagGet(AutoFeed_Flag))
                    {
                        Page_FlagSet(PageAutoFeed_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageAutoFeed_Flag);
                    }
                    if(Main_FlagGet(Sound_Flag))
                    {
                        Page_FlagSet(PageSound_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageSound_Flag);
                    }
                    if(Main_FlagGet(ChildLock_Flag))
                    {
                        Page_FlagSet(PageChildLock_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageChildLock_Flag);
                    }
                }
                /* 设置页向主页 */
                else if(Main_FlagGet(SelectPage_Flag))
                {
                    Main_FlagSet(Index_Flag);
                    Main_FlagReset(SelectPage_Flag);
                    OLED_Clear();
                    Page_indexInit();
                }
                /* 放粮时间设置页到设置页 */
                else if(Main_FlagGet(SetFeedTimePage_Flag))
                {
                    Main_FlagReset(SetFeedTimePage_Flag);
                    Main_FlagSet(SelectPage_Flag);
                    Page_selectInit();
                    page_select = 0;
                    if(Main_FlagGet(AutoFeed_Flag))
                    {
                        Page_FlagSet(PageAutoFeed_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageAutoFeed_Flag);
                    }
                    if(Main_FlagGet(Sound_Flag))
                    {
                        Page_FlagSet(PageSound_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageSound_Flag);
                    }
                    if(Main_FlagGet(ChildLock_Flag))
                    {
                        Page_FlagSet(PageChildLock_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageChildLock_Flag);
                    }
                    main_savedata();
                }
                /* 放粮时间设置页到设置页 */
                else if(Main_FlagGet(SetFeedSizePage_Flag))
                {
                    Main_FlagReset(SetFeedSizePage_Flag);
                    Main_FlagSet(SelectPage_Flag);
                    Page_selectInit();
                    page_select = 0;
                    if(Main_FlagGet(AutoFeed_Flag))
                    {
                        Page_FlagSet(PageAutoFeed_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageAutoFeed_Flag);
                    }
                    if(Main_FlagGet(Sound_Flag))
                    {
                        Page_FlagSet(PageSound_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageSound_Flag);
                    }
                    if(Main_FlagGet(ChildLock_Flag))
                    {
                        Page_FlagSet(PageChildLock_Flag);
                    }
                    else
                    {
                        Page_FlagReset(PageChildLock_Flag);
                    }
                    main_savedata();
                }
            }
            else if(k == SELECT_KEY && !Main_FlagGet(ChildLock_Flag))
            {
                /* 设置页选择按键 */
                if(Main_FlagGet(SelectPage_Flag))
                {
                    page_select++;
                    page_select = page_select % 5;
                }
                /* 喂养时间页面选择按键 */
                else if(Main_FlagGet(SetFeedTimePage_Flag))
                {
                    feed_time_select++;
                    feed_time_select = feed_time_select % 4;
                }
                /* 放粮数量页面选择按键 */
                else if(Main_FlagGet(SetFeedSizePage_Flag))
                {
                    feed_time_select++;
                    if(feed_time_select==3)
                    {
                        feed_time_select = 1;
                    }
                    feed_time_select = feed_time_select % 3;
                }
            }
            else if(k == POWER_KEY && !Main_FlagGet(ChildLock_Flag))
            {
                // Main_FlagSet(MusicUp_Flag);
                // Main_FlagSet(MusicDown_Flag);
                // main_savedata();
                if(Main_FlagGet(FoodOpenWorking_Flag) || Main_FlagGet(WaterOpenWorking_Flag))
                {
                    Ctrl_foodOpen(false);
                    Ctrl_waterOpen(false);
                }
                else Main_FlagShift(forceOpen_Flag);
            }
            else if(Main_FlagGet(ChildLock_Flag) && k != notouch)
            {
                if(k == CONFIRM_KEY && child_unlock == 0)
                {
                    child_unlock++;
                }
                else if(k == SELECT_KEY && child_unlock == 1)
                {
                    child_unlock++;
                }
                else if(k == POWER_KEY && child_unlock == 2)
                {
                    child_unlock++;
                }
                else
                {
                    child_unlock = 0;
                }
                if(child_unlock == 3)
                {
                    Main_FlagReset(ChildLock_Flag);
                    Page_FlagReset(PageChildLock_Flag);
                }
            }
            key_scan_mills = Clock_mills();
            i++;
        }
        
        /* 缺粮缺水提示部分 */
        if(Clock_mills() - no_food_detect_time > 500)
        {
            Hint_set(NO_FOOD, Infrared_state());
            no_food_detect_time = Clock_mills();
            Hint_set(NO_WATER, (water_state==0xF0));
        }

        /* 声音提示部分 */
        if(sound_flag)
        {
            sound_time = Clock_mills();
            Hint_sound(true);
            sound_flag = false;
        }
        else if(Clock_mills() - sound_time > 100 && music_up == 5 && music_down == 5)
        {
            Hint_sound(false);
        }

        if(Main_FlagGet(MusicUp_Flag))
        {
            music_up = 0;
            Main_FlagReset(MusicUp_Flag);
            music_time = Clock_mills();
        }
        else if(music_up < 5 && Clock_mills() - music_time > 200)
        {
            if(music_up == 4)
            {
                Hint_sound(false);
                music_up++;
            }
            if(music_up < 4)
            {
                Hint_music(music_up, true);
                music_up++;
                music_time = Clock_mills();
            }
        }
        if(Main_FlagGet(MusicDown_Flag))
        {
            music_down = 0;
            Main_FlagReset(MusicDown_Flag);
            music_time = Clock_mills();
        }
        else if(music_down < 5 && Clock_mills() - music_time > 200)
        {
            if(music_down == 4)
            {
                Hint_sound(false);
                music_down++;
            }
            if(music_down < 4)
            {
                Hint_music(music_down, false);
                music_down++;
                music_time = Clock_mills();
            }
        }

        /* 自动控制部分 */
        if(Main_FlagGet(AutoFeed_Flag) && Clock_mills() - last_feedtime > 1000)
        {
            if(main_time2feed() && !Main_FlagGet(FoodOpenWorking_Flag) && !Main_FlagGet(WaterOpenWorking_Flag))
            {
                Main_FlagSet(FoodOpen_Flag);
                Main_FlagSet(WaterOpen_Flag);
                Main_FlagSet(FoodOpenWorking_Flag);
                Main_FlagSet(WaterOpenWorking_Flag);
                last_feedtime = Clock_mills();
            }
        }

        /* 控制部分 */
        if(Main_FlagGet(forceOpen_Flag))
        {
            Ctrl_foodOpen(true);
        }
        else if(Main_FlagGet(FoodOpen_Flag))
        {
            Ctrl_foodOpen(true);
            Main_FlagReset(FoodOpen_Flag);
            feed_time = Clock_mills();
        }
        else if(Clock_mills() - feed_time > 1000*feedSize[0])
        {
            Ctrl_foodOpen(false);
            Main_FlagReset(FoodOpenWorking_Flag);
        }

        
        if(Main_FlagGet(forceOpen_Flag))
        {
            Ctrl_waterOpen(32000000/800);
        }
        else if(Main_FlagGet(WaterOpen_Flag))
        {
            Ctrl_waterOpen(32000000/800);
            Main_FlagReset(WaterOpen_Flag);
            water_time = Clock_mills();
        }
        else if(water_state & BIT(feedSize[1]-1) || Clock_mills() - water_time > 30000)
        {
            Ctrl_waterOpen(false);
            Main_FlagReset(WaterOpenWorking_Flag);
        }

        // Ctrl_foodOpen(Main_FlagGet(FoodOpen_Flag));
        // Ctrl_waterOpen(Main_FlagGet(FoodOpen_Flag)*32000000/800);

        /* 器件通讯部分 */
        if(Communicate_available(WaterDetector_Uart))
        {
            uint8_t str_water[2];
            Communicate_receiveString8(WaterDetector_Uart, str_water, 2);
            water_state = str_water[0];
        }
        if(Clock_mills() - com_mills > 10)
        {
            main_dealCommunicate(ESP32_Uart);
            main_dealCommunicate(Computer_Uart);
            com_mills = Clock_mills();
        }

        /* OLED显示部分 */
        if(Main_FlagGet(Index_Flag))
        {
            Page_indexTime(nowtime[0], nowtime[1], nowtime[2]);
        }
        else if(Main_FlagGet(SelectPage_Flag))
        {
            Page_showSelect(page_select);
        }
        else if(Main_FlagGet(SetFeedTimePage_Flag))
        {
            Page_showSetFeedTime(feed_time_select, feedTime);
        }
        else if(Main_FlagGet(SetFeedSizePage_Flag))
        {
            Page_showSetFeedSize(feed_time_select, feedSize);
        }

        // OLED_ShowNum(1,1,Clock_mills(),16);
    }
}

// uint8_t feedTime[][3] = {{8, 0, 0},{12, 0, 0},{16, 53, 58}};
// uint8_t feedSize[2] = {9, 4};

// #define AutoFeed_Flag (BIT(3))

// #define FeedTime0On_Flag (BIT(6))
// #define FeedTime1On_Flag (BIT(7))
// #define FeedTime2On_Flag (BIT(8))

void main_savedata()
{
    uint8_t i, j;
    uint8_t flags = 0;

    IAP_SectorErase(IAP_MEMTYPE_EEPROM, 0, 0x40);
    // Save_erase(EEPROM_Sector0);

    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            IAP_ProgramByte(3*i+j, feedTime[i][j], IAP_MEMTYPE_EEPROM, 0x40);
            // Save_write(EEPROM_Sector0+3*i+j, feedTime[i][j]);// 0~8
    
    for(i=0;i<2;i++)
            IAP_ProgramByte(9+i, feedSize[i], IAP_MEMTYPE_EEPROM, 0x40);
        // Save_write(EEPROM_Sector0+9+i, feedSize[i]);// 9~10
    
    flags = Main_FlagGet(AutoFeed_Flag);
    flags |= Main_FlagGet(FeedTime0On_Flag) << 1;
    flags |= Main_FlagGet(FeedTime1On_Flag) << 2;
    flags |= Main_FlagGet(FeedTime2On_Flag) << 3;

    IAP_ProgramByte(11, flags, IAP_MEMTYPE_EEPROM, 0x40);
}

void main_readdata()
{
    uint8_t i, j;
    uint8_t flags = 0;
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            feedTime[i][j] = IAP_ReadByte(3*i+j, IAP_MEMTYPE_EEPROM);
            // feedTime[i][j] = Save_read(EEPROM_Sector0+3*i+j);// 0~8
    
    for(i=0;i<2;i++)
        feedSize[i] = IAP_ReadByte(9+i, IAP_MEMTYPE_EEPROM);
        // feedSize[i] = Save_read(EEPROM_Sector0+9+i);// 9~10
    
    flags = IAP_ReadByte(11, IAP_MEMTYPE_EEPROM);
    // flags = Save_read(EEPROM_Sector0+11);

    if(flags & 1)
        Main_FlagSet(AutoFeed_Flag);
    if(flags & 2)
        Main_FlagSet(FeedTime0On_Flag);
    if(flags & 4)
        Main_FlagSet(FeedTime1On_Flag);
    if(flags & 8)
        Main_FlagSet(FeedTime2On_Flag);
}

void main_dealCommunicate(uint8_t now_Uart)
{
    uint8_t i=0;
    uint8_t str_size;
    if(Communicate_available(now_Uart) > 4)
    {
        if(str_size = Communicate_receiveString8(now_Uart, str, 50))
        {
            for(i=0;i<str_size;i++)
            {
                if(str[i]=='c' && str[i+1]=='m' && str[i+2]=='d' && str[i+3]=='+')
                {
                    i=i+4;
                    /* cmd+data */
                    if(str[i] == 'd' && str[i+1] == 'a' && str[i+2] == 't' && str[i+3] == 'a')
                    {
                        Communicate_sendString8(now_Uart, "data:");
                        Communicate_sendData8(now_Uart, Main_FlagGet(AutoFeed_Flag));
                        Communicate_sendData8(now_Uart, feedTime[0][0]);
                        Communicate_sendData8(now_Uart, feedTime[0][1]);
                        Communicate_sendData8(now_Uart, feedTime[0][2]);
                        Communicate_sendData8(now_Uart, feedTime[1][0]);
                        Communicate_sendData8(now_Uart, feedTime[1][1]);
                        Communicate_sendData8(now_Uart, feedTime[1][2]);
                        Communicate_sendData8(now_Uart, feedTime[2][0]);
                        Communicate_sendData8(now_Uart, feedTime[2][1]);
                        Communicate_sendData8(now_Uart, feedTime[2][2]);
                        Communicate_sendData8(now_Uart, Main_FlagGet(FeedTime0On_Flag));
                        Communicate_sendData8(now_Uart, Main_FlagGet(FeedTime1On_Flag));
                        Communicate_sendData8(now_Uart, Main_FlagGet(FeedTime2On_Flag));
                        Communicate_sendData8(now_Uart, feedSize[0]);
                        Communicate_sendData8(now_Uart, feedSize[1]);
                    }
                    /* cmd+auto */
                    else if(str[i] == 'a' && str[i+1] == 'u' && str[i+2] == 't' && str[i+3] == 'o')
                    {
                        Main_FlagShift(AutoFeed_Flag);
                        Page_FlagShift(PageAutoFeed_Flag);
                        if(Main_FlagGet(AutoFeed_Flag))
                        {
                            Communicate_sendString8(now_Uart, "auto_on");
                            Main_FlagSet(MusicUp_Flag);
                        }
                        else
                        {
                            Communicate_sendString8(now_Uart, "auto_off");
                            Main_FlagSet(MusicDown_Flag);
                        }
                        main_savedata();
                    }
                    /* cmd+feed */ /* <0 h m s> */ /* <1 h m s> */ /* <2 h m s> */
                    else if(str[i] == 'f' && str[i+1] == 'e' && str[i+2] == 'e' && str[i+3] == 'd')
                    {
                        uint8_t i4 = str[i+4] - '0';
                        if(i4 < 3)
                        {
                            feedTime[i4][0] = str[i+5] - '0';
                            feedTime[i4][1] = str[i+6] - '0';
                            feedTime[i4][2] = str[i+7] - '0';
                            Communicate_sendString8(now_Uart, "setOK");
                            Page_feedTimeFresh();
                            Main_FlagSet(MusicUp_Flag);
                            main_savedata();
                        }
                        else
                        {
                            Communicate_sendString8(now_Uart, "setWRONG");
                        }
                    }
                    /* cmd+turn */ /* <0> */ /* <1> */ /* <2> */
                    else if(str[i] == 't' && str[i+1] == 'u' && str[i+2] == 'r' && str[i+3] == 'n')
                    {
                        uint8_t i4 = str[i+4] - '0';
                        switch (i4)
                        {
                        case 0:
                            Main_FlagShift(FeedTime0On_Flag);
                            Page_FlagShift(PageFeedTime0On_Flag);
                            Communicate_sendString8(now_Uart, "setOK");
                            Main_FlagSet(MusicUp_Flag);
                            main_savedata();
                            break;
                        case 1:
                            Main_FlagShift(FeedTime1On_Flag);
                            Page_FlagShift(PageFeedTime1On_Flag);
                            Communicate_sendString8(now_Uart, "setOK");
                            Main_FlagSet(MusicDown_Flag);
                            main_savedata();
                            break;
                        case 2:
                            Main_FlagShift(FeedTime2On_Flag);
                            Page_FlagShift(PageFeedTime2On_Flag);
                            Communicate_sendString8(now_Uart, "setOK");
                            Main_FlagSet(MusicUp_Flag);
                            main_savedata();
                            break;
                        default:
                            Communicate_sendString8(now_Uart, "setWRONG");
                        }
                    }
                    else if(str[i] == 's' && str[i+1] =='i' && str[i+2] == 'z' && str[i+3] == 'e')
                    {
                        uint8_t i4 = str[i+4] - '0';
                        uint8_t size = str[i+5];
                        if ((i4 == 0 && size - 1 < 9) || (i4 == 1 && size - 1 < 4))
                        {
                            feedSize[i4] = size;
                            Communicate_sendString8(now_Uart, "setOK");
                            Main_FlagSet(MusicUp_Flag);
                            Page_feedSizeFresh();
                            main_savedata();
                        }
                        else
                            Communicate_sendString8(now_Uart, "setWRONG");
                    }
                    /* cmd+time */ /* <h m s> */
                    else if(str[i] == 't' && str[i+1] == 'i' && str[i+2] == 'm' && str[i+3] == 'e')
                    {
                        nowtime[0] = str[i+4] - '0';
                        nowtime[1] = str[i+5] - '0';
                        nowtime[2] = str[i+6] - '0';
                    }
                }
            }
        }
    }
}

bool main_time2feed()
{
    bool feedtimeon[3];
    uint8_t i;
    uint32_t daytime;
    uint32_t nowdaytime = nowtime[0]*60*24+nowtime[1]*60+nowtime[2];
    feedtimeon[0] = Main_FlagGet(FeedTime0On_Flag);
    feedtimeon[1] = Main_FlagGet(FeedTime1On_Flag);
    feedtimeon[2] = Main_FlagGet(FeedTime2On_Flag);
    for(i=0;i<3;i++)
    {
        daytime = feedTime[i][0]*60*24+feedTime[i][1]*60+feedTime[i][2];
        if(feedtimeon[i] && ((nowdaytime - daytime) % 86400)<10)
        {
            return true;
        }
    }
    return false;
}

